package com.pkk.penguin.frame.know.shiro.shirodemo.springversion.service;

import com.pkk.penguin.frame.know.shiro.shirodemo.springversion.dbsource.TbUserDbService;
import com.pkk.penguin.frame.know.shiro.shirodemo.springversion.entity.TbUserEntity;
import com.pkk.penguin.frame.know.shiro.shirodemo.springversion.entity.resource.ResourceRole;
import com.pkk.penguin.frame.know.shiro.shirodemo.springversion.entity.role.RoleEntity;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @description: 用户业务层
 * @author: peikunkun
 * @create: 2018-08-22 10:22
 **/
@Service
@Slf4j
public class TbUserService {

  @Resource
  private TbUserDbService tbUserDbService;

  /**
   * @Description: 根据账户获取用户信息
   * @Param: a
   * @return: a
   * @Author: peikunkun
   * @Date: 2018/8/22 0022 下午 5:33
   */
  public TbUserEntity selectUserInfoByName(String uName) {
    return tbUserDbService.selectUserInfoByName(uName);
  }

  /**
   * @Description: 根据账户id获取用户信息
   * @Param: a
   * @return: a
   * @Author: peikunkun
   * @Date: 2018/8/22 0022 下午 5:33
   */
  public TbUserEntity selectUserInfoById(Long id) {
    return tbUserDbService.selectUserInfoById(id);
  }


  /**
   * @Description: 更新最后登录时间
   * @Param: [id]
   * @return: void
   * @Author: peikunkun
   * @Date: 2018/8/23 0023 下午 12:46
   */
  public void updateUserLastLoginTimeById(Long id) {
    Integer updateResult = tbUserDbService.updateUserLastLoginTimeById(id);
    log.info("更新最后时间结果为：" + (updateResult >= 1));
  }

  /**
   * @Description: 查询当前用户的角色
   * @Param: [userId]
   * @return: java.util.List<com.pkk.com.pkk.penguin.frame.know.shiro.shirodemo.springversion.entity.user.RoleEntity>
   * @Author: peikunkun
   * @Date: 2018/8/29 0029 下午 1:16
   */
  public List<RoleEntity> selectUserRoleByUserId(Long userId) {
    return tbUserDbService.selectUserRoleByUserId(userId);
  }

  /**
   * @Description: 查询当前用户的角色
   * @Param: [userId]
   * @return: java.util.List<com.pkk.com.pkk.penguin.frame.know.shiro.shirodemo.springversion.entity.user.RoleEntity>
   * @Author: peikunkun
   * @Date: 2018/8/29 0029 下午 1:16
   */
  public List<RoleEntity> selectUserRoleByUserName(String userName) {
    return tbUserDbService.selectUserRoleByUserName(userName);
  }

  /**
   * @Description: 获取所有的菜单
   * @Param: []
   * @return: java.util.List<com.quzeng.oauth.common.response.external.ResourceRole>
   * @Author: peikunkun
   * @Date: 2018/7/24 0024 下午 4:46
   */
  public List<ResourceRole> getAllMenu() {
    List<ResourceRole> resourceRoles = tbUserDbService.getAllResource();

    //根据一级菜单id查询所有的菜单
    List<ResourceRole> userMenuVos = new ArrayList<ResourceRole>();
    for (ResourceRole ResourceRole : resourceRoles) {
      //这里需要的是一级菜单的id,按照level来进行判断是否为一级菜单
      if (ResourceRole.getParentResourceId() != null && ResourceRole.getResourceLevel() == 1) {
        List<ResourceRole> iterateMenus = iterateMenus(resourceRoles, ResourceRole.getResourceId());
        ResourceRole.setChild(iterateMenus);
        userMenuVos.add(ResourceRole);
      }
    }
    return userMenuVos;
  }

  /**
   * @Description: 获取所有的菜单
   * @Param: []
   * @return: java.util.List<com.quzeng.oauth.common.response.external.ResourceRole>
   * @Author: peikunkun
   * @Date: 2018/7/24 0024 下午 4:46
   */
  public List<ResourceRole> getAllResourceByRoles(Long[] roles) {
    List<ResourceRole> resourceRoles = tbUserDbService.getAllResourceByRoles(roles);

    //根据一级菜单id查询所有的菜单
    List<ResourceRole> userMenuVos = new ArrayList<ResourceRole>();
    for (ResourceRole ResourceRole : resourceRoles) {
      //这里需要的是一级菜单的id,按照level来进行判断是否为一级菜单
      if (ResourceRole.getParentResourceId() != null && ResourceRole.getResourceLevel() == 1) {
        List<ResourceRole> iterateMenus = iterateMenus(resourceRoles, ResourceRole.getResourceId());
        ResourceRole.setChild(iterateMenus);
        userMenuVos.add(ResourceRole);
      }
    }
    return userMenuVos;
  }


  /**
   * @Description:多级菜单查询方法
   * @Param: [menuVoList, pid:要传入的资源id]
   * @return: java.util.List<com.quzeng.oauth.common.response.external.ResourceRole>
   * @Author: peikunkun
   * @Date: 2018/7/24 0024 下午 7:07
   */
  public List<ResourceRole> iterateMenus(List<ResourceRole> menuVoList, Long pid) {
    List<ResourceRole> result = new ArrayList<ResourceRole>();
    for (ResourceRole tbResourceRole : menuVoList) {
      Long resourceId = tbResourceRole.getResourceId();//获取菜单的id
      Long resourcePid = tbResourceRole.getParentResourceId();//获取菜单的父id
      if (resourcePid != null) {
        if (resourcePid.equals(pid)) {
          List<ResourceRole> iterateMenu = iterateMenus(menuVoList, resourceId);
          tbResourceRole.setChild(iterateMenu);
          result.add(tbResourceRole);
        }
      }
    }
    return result;
  }

}
